<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\Request;
use app\admin\service\WholesalAssets as WholesalAssetsService;
use app\admin\validate\WholesalAssets as WholesalAssetsValidate;

class WholesalAssets
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request, WholesalAssetsService $service, WholesalAssetsValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['product_name'] =  $request->param('product_name',null);
        $param['code'] =  $request->param('code',null);
        $param['brand'] =  $request->param('brand',null);
        $param['category'] =  $request->param('category',null);
        $param['currency'] =  $request->param('currency',null);
        $param['user_id'] =  $request->param('user_id',null);
        $param['wid'] =  $request->param('wid',null);
        $param['is_futures'] =  $request->param('is_futures',null);

        $param['start_time'] =  $request->param('start_time',null);
        $param['end_time'] =  $request->param('end_time',null);

        $intPage =  $request->param('pageIndex',1);
        $intPageSize =  $request->param('pageSize',50);


        if ($param['user_id'] == null && !isSuperAdmin($request->operator_id)) { //普通用户
            $operatorService = new \app\admin\service\Operator();
            $userResult = $operatorService->getBelongCustomers($request->operator_id);
            if (0 == $userResult['code']) {
                if (1 == $userResult['data']['order_range']) {
                    $param['user_id'] =  $userResult['data']['customer_ids'];
                }
            } else {
                $param['user_id'] =  [0];
            }
        }

        $arrData = $service->index($param,(int)$intPage,(int)$intPageSize);
        return json($arrData);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    /**
     * 获取资源
     * @param Request $request
     * @param WholesalAssetsService $WholesalAssetsService
     * @param WholesalAssetsValidate $validate
     * @return void
     * 2024-11-13 13 46 02
     * author: jiang
     */
    public function resource(Request $request, WholesalAssetsService $service, WholesalAssetsValidate $validate)
    {
        $params = $request->param();
        $arrData = $service->resource($params['field'] ?? null);
        return json($arrData);
    }
}
